package main

import (
	"global/core"
	"global/model"
	"os"
	"fmt"
)

type Kline struct {
	ctx 		*core.Context
	exchange 	*model.Exchange
	slug 		string
}

func NewKline(ctx *core.Context, slug string) *Kline {
	kline :=  &Kline{
		ctx:		ctx,
		slug: 		slug,
	}
	return kline
}

func (k *Kline) LoadExchange()  {
	has, exgInfo, err := model.ExchangeModel.GetItemBySlug(k.slug)
	if !has {
		fmt.Println("Get exchange by slug failed, err:", err.Error())
		os.Exit(1)
	}
	k.exchange = exgInfo
}

func (k *Kline) Serv() {
	f := KlineFactory(k.slug)
	if f == nil {
		fmt.Println("Error: slug not supported in factory, slug:", k.slug)
		os.Exit(1)
	}

	if err := f.Init(k.ctx, k.slug); err != nil {
		fmt.Println("Error: init failed, err:", err.Error())
		os.Exit(1)
	}

	f.Run()
}
